<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="./css/inputnumber.css" />
  <link rel="stylesheet" href="./css/index.css" />
  <title>购物车</title>
</head>

<body>
  <div class="app-container" id="app">
    <!-- 顶部banner -->
    <div class="banner-box"><img src="http://autumnfish.cn/static/fruit.jpg" alt="" /></div>
    <!-- 面包屑 -->
    <div class="breadcrumb">
      <span>🏠</span>
      /
      <span>购物车</span>
    </div>
    <!-- 购物车主体 -->
    <div class="main">
      <div class="table">
        <!-- 头部 -->
        <div class="thead">
          <div class="tr">
            <div class="th">选中</div>
            <div class="th th-pic">图片</div>
            <div class="th">单价</div>
            <div class="th num-th">个数</div>
            <div class="th">小计</div>
            <div class="th">操作</div>
          </div>
        </div>
        <!-- 身体 -->
        <div class="tbody">

          <!-- 渲染需要的行 -->
          <!-- 
            <div class="tr">
            <div class="td"><input type="checkbox" checked /></div>
            <div class="td"><img src="http://autumnfish.cn/static/火龙果.png" alt="" /></div>
            <div class="td">6</div>
            <div class="td">
              <div class="my-input-number">
                <button class="decrease"> - </button>
                <span class="my-input__inner">2</span>
                <button class="increase"> + </button>
              </div>
            </div>
            <div class="td">12</div>
            <div class="td"><button class="del">删除</button></div>
          </div> 
        -->

        </div>
      </div>
      <!-- 底部 -->
      <div class="bottom">
        <!-- 全选 -->
        <label class="check-all">
          <input type="checkbox" />
          全选
        </label>
        <div class="right-box">
          <!-- 所有商品总价 -->
          <span class="price-box">总价&nbsp;&nbsp;:&nbsp;&nbsp;¥&nbsp;<span class="price">24</span></span>
          <!-- 结算按钮 -->
          <button class="pay">结算( 6 )</button>
        </div>
      </div>
    </div>
    <!-- 空车 -->
    <div class="empty">🛒空空如也</div>
  </div>
  <script>
    const dataArr = [
      {
        id: 1,
        icon: 'http://autumnfish.cn/static/火龙果.png',
        isChecked: true,
        num: 2,
        price: 6,
      },
      {
        id: 2,
        icon: 'http://autumnfish.cn/static/荔枝.png',
        isChecked: false,
        num: 7,
        price: 20,
      },
      {
        id: 3,
        icon: 'http://autumnfish.cn/static/榴莲.png',
        isChecked: false,
        num: 3,
        price: 40,
      },
      {
        id: 4,
        icon: 'http://autumnfish.cn/static/鸭梨.png',
        isChecked: true,
        num: 10,
        price: 3,
      },
      {
        id: 5,
        icon: 'http://autumnfish.cn/static/樱桃.png',
        isChecked: false,
        num: 20,
        price: 34,
      },
    ]

   
    //渲染页面,render函数封装
    function render(arr) {
      const strArr = arr.map(function (ele, index) {
        const { id, icon, isChecked, num, price } = ele
        return `
      <div class="tr" data-id="${index}">                          
                                            
            <div class="td"><input type="checkbox" ${isChecked ? `checked` : ``} /></div>
            <div class="td"><img src="${icon}" alt="" /></div>
            <div class="td">${price}</div>
            <div class="td">
              <div class="my-input-number">
                <button class="decrease"> - </button>
                <span class="my-input">${num}</span>
                <button class="increase" > + </button>
              </div>
            </div>
            <div class="td">${price * num}</div>
            <div class="td"><button class="del"  >删除</button></div>
      </div> 
      `
      })
      document.querySelector(`.tbody`).innerHTML = strArr.join(``)

      //全选反选功能
      //获取到全选复选框
      const checkAll = document.querySelector(`.check-all input`)
      //  console.log(checkAll)
      //获取到小的复选框
      let checksbs = document.querySelectorAll(`.td  input`)
    
      checksbs = Array.from(checksbs)

      checkAll.addEventListener(`click`, function () {
        checksbs.forEach(item => item.checked = this.checked)
      })


      //小的复选框全部点选的情况下，大的全选按钮也会点选上
     
      checksbs.forEach(item => item.addEventListener(`click`, function () {
        checkAll.checked = checksbs.every(ele => ele.checked)
      }))


      //计算总额和总数量的前提是在勾选的情况下
      const pay = document.querySelector(`.pay`)
      // console.log(pay)
      const price = document.querySelector(`.price`)
      //  console.log(price)
      const newArr = dataArr.filter(ele => ele.isChecked===true)
       
      price.innerHTML = newArr.reduce((prev, item) => prev + item.price * item.num, 0)
      pay.innerHTML = `结算(${newArr.reduce((prev, item) => prev + item.num, 0)})`
   
      //如果没有任何勾选的情况下，购物篮显示，不然为``
      const empty = document.querySelector(`.empty`)
      if (dataArr.length === 0) {
        empty.style.display=`block`
      }else{
        empty.style.display=`none`
      
      }
    }
    render(dataArr)
    // console.log(dataArr)



    //购买的个数可以点击新增和点击减少功能
    const myInput = document.querySelector(`.my-input`)
    // console.log(myInput)
    const decrease = document.querySelector(`.decrease`)
    const increase = document.querySelector(`.increase`)
    // console.log(increase)
    const tbody = document.querySelector(`.tbody`)
    const del = document.querySelector(`.del`)
    // console.log(del)

    //点击加号的情况下，加号前面的值会增加,注意每一行都设置的有自定义属性下标值获取到
    tbody.addEventListener(`click`, function (e) {
      //如果点选到加号，加号哪一行的索引下标值将会增加
      if (e.target.className === `increase`) {
        const idNum = e.target.parentNode.parentNode.parentNode.dataset.id
        // console.log(idNum)
        dataArr[idNum].num++
        render(dataArr)
      }

      //同样的减少，但是注意到一就不能继续减了
      if (e.target.className === `decrease`) {
        const idNum = e.target.parentNode.parentNode.parentNode.dataset.id

        if (dataArr[idNum].num <= `1`) {
          dataArr[idNum].num = 1
        } else {
          dataArr[idNum].num--
          render(dataArr)
        }
      }
      //删除功能

      if (e.target.className === `del`) {
        const id = e.target.parentNode.parentNode.dataset.id
        if (!confirm(`您确定删除吗`)) {
          return
        } else { dataArr.splice(id, 1) }
        render(dataArr)
      }
      
      if (e.target.tagName === 'INPUT') {
        const id = e.target.parentNode.parentNode.dataset.id
        dataArr[id].isChecked = e.target.checked
        render()
      }
     
      

    })






  </script>    
</body>

</html>